
$(function(){
	new Vue({
		el:"#shopGoodTypeApp",
		data:{
			fullscreenLoading:false,
			activeName: 'first',
			title:"新增一级分类",
			formLabelWidth:"120px",
			dialogFormVisible:false,
			shopGoodPlatformData:[],
			dialogEditFormVisible:false,
			uploadUrl: "http://39.100.131.147:8888/goods/address/uploadFile",
			carouseImgUrl:[],
			form:{
				name:"",
			    showOrder:0,
			},
			editFrom:{
				name:"",
			},
		},
		created:function(){
			parent.vueData.$data.hreHtml="../goodFristType/index.html";
			parent.vueData.activeIndex  = "6";
			parent.vueData.getLeftNav("6")
			parent.vueData.$data.categoryIndex = 2
			this.getShopList()
		},
		methods:{
			getShopList(){
               this.fullscreenLoading = true;
               axios({
               	url: POSTURL.selectAllPrimaryCategories,
               	method: 'post',
               	data: {},
               }).then(response => {
               	this.fullscreenLoading = false;
               	if (response.data.code === 100) {
               		this.shopGoodPlatformData = response.data.result;
               	} else {
               		this.$message.error(response.data.message);
               	}
               }).catch(error => {
               	this.fullscreenLoading = false;
               	console.log(error);
               	this.$message.error('服务器出错!');
               })
			},
			
			addPlartFristCategory(){
				this.title= "新增平台一级分类";
				this.dialogFormVisible =true;
				this.form = {};
				this.carouseImgUrl = [];
			},
			handleClick(tab, event) {
				console.log(tab.name);
			},
			 beforeHandleCommand(row,command){
			            return {
			                'row': row,
			                'command':command
			            }
			        },
		
			handlePlatformCommand(command){
				if(command.command==="a"){
			    this.carouseImgUrl = [];
				this.form = {}
				this.form.parentId = command.row.uid
			    this.title= "添加二级分类";
			    this.dialogFormVisible =true;
				}
				else if (command.command==="b"){
                localStorage.setItem("goodCategoryId", command.row.uid);
                 parent.vueData.handleOpen(63,1)
				}
				
			},
			handleEdit(index,row){
				this.title= "编辑一级分类";
				this.dialogFormVisible =true;
			},
			addFristCategory(){
			   this.title= "新增一级分类";
			   this.dialogFormVisible =true;
			},
			beforeCourseRemove(file, fileList) {
				  let index = this.carouseImgUrl.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.carouseImgUrl.splice(index, 1)
				return true;
			},
			handleCoverExceed(files, fileList) {
				this.$message.warning("分类图片只能上传一张图片")
			},
			handleCoverSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.carouseImgUrl.push({
						name: "分类图片",
			            url: response.result,
					})
					
				}
			},
			configUpload(){
				this.addSecondGoodType()
				
			},
			isNotNumber(inputData) {
				if (parseFloat(inputData).toString() == "NaN") {
					return false;
				} else {
					return true;
				}
			},
			addSecondGoodType(){
				if(stringEmpty(this.form.name)){
					this.$message.error("分类名称不能为空");
					return;
				}
				if(!this.isNotNumber(this.form.showOrder)){
					this.$message.error("分类权重只能为数字");
					return;
				}
				if(this.carouseImgUrl.length<=0){
					this.$message.error("分类图片不能为空");
					return;
				}
				this.dialogFormVisible = false;
				this.fullscreenLoading = true;
				this.form.photo  = this.carouseImgUrl[0].url
				axios({
					url: POSTURL.insertCategory,
					method: 'post',
					data: this.form
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						if(stringEmpty(this.form.parentId)){
							this.getShopList();
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			configEditUpload(){
				if(stringEmpty(this.editFrom.name)){
					this.$message.error("分类名称不能为空");
					return;
				}
				if(this.carouseImgUrl.length<=0){
					this.$message.error("分类图片不能为空");
					return;
				}
				this.editFrom.photo  = this.carouseImgUrl[0].url
				axios({
					url: POSTURL.editGoodType,
					method: 'post',
					data: this.editFrom
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						this.dialogEditFormVisible = false;
						this.getShopList();
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
				
			},
			editGoodType(val){
				console.log(val);
				this.dialogEditFormVisible = true;
				this.carouseImgUrl= [];
				this.carouseImgUrl.push({
					name: "分类图片",
				    url: val.photo,
				})
				this.editFrom.name = val.name
				this.editFrom.categoryId = val.uid
				
			}
			
		}
	})
})